<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="刘清政">
  <meta name="keyword" content="hexo-theme">
  
    <link rel="shortcut icon" href="/css/images/logo.png">
  
  <title>
    
      db/Elasticsearch系列/18-Elasticsearch之打分机制 | Justin-刘清政的博客
    
  </title>
  <link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/tomorrow.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/css/plugins/gitment.css">

  
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
    
<script src="/js/gitment.js"></script>

  
  

  
<meta name="generator" content="Hexo 4.2.0"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>

  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>Justin-刘清政的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于我</a>
          
        </li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于我</a>
            
          </li>
        
      </ul>
    </div>
  </div>
</header>

    <div id="article-banner">
  <h2>db/Elasticsearch系列/18-Elasticsearch之打分机制</h2>



  <p class="post-date">2019-12-30</p>
    <!-- 不蒜子统计 -->
    <span id="busuanzi_container_page_pv" style='display:none' class="">
        <i class="icon-smile icon"></i> 阅读数：<span id="busuanzi_value_page_pv"></span>次
    </span>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><h2 id="一-例子"><a href="#一-例子" class="headerlink" title="一 例子"></a>一 例子</h2><p>现在，讲述一个真实的故事！<br> 故事一定是伴随着赵忠祥老师的声音开始的，雨季就要来临了，又到了动物们发情的季节了…<br> 还记得，之前发生的作家六六吐槽xx的事情吗？对了，有图有真相！上图上图：</p>
<p><img src="https://img2018.cnblogs.com/blog/1168165/201904/1168165-20190417001048486-214033720.png" alt="img"></p>
<p>身为吃瓜群众，要从专业的角度来分析，就事论事哈：<br> 就搜索结果本身而言，xx返回了正确的结果（是的，人家已经调整了，现在搜没问题！）。因为返回的结果中，都包含了搜索的关键字。而我们从逻辑上来看，这他娘的一堆广告算是咋回事！这个吐槽是从用户的角度出发的。很显然，返回的结果中，尤其是前几条，有时甚至是前几页，都跟我们想要的结果相差深远！<br> 进一步说，仅仅以二元的方式来考虑文档和查询的匹配可能是有意义的，也就是百度搜索引擎返回了二元的匹配结果：是的，找到了，不，老娘没找到！虽然返回了结果，其中也包含了我们想要的结果，即便你要在大堆的广告中找正确的结果实属不易，但就像大家都习惯了广告中插播电视剧一样，习惯就好嘛！xx从x的角度出发，为广告的词条增加权重，至于那个真正的结果，我擦，你也没给我钱……..<br> 而需要xx才能访问的xx浏览器，在正确的给用户返回二元结果之前，更多的考虑文档的相关性（relevancy），因为就某个结果而言，如果A文档要比B文档更和结果相关，那么A文档在结果中就要比B文档靠前，再加上以其他的优化，最终将所有结果返回，而用户最期待的那条结果很可能排在最高位，这岂不美哉？<br> <strong>确定文档和查询有多么相关的过程被称为打分（scoring）</strong>。</p>
<h2 id="二-文档打分的运作机制：TF-IDF"><a href="#二-文档打分的运作机制：TF-IDF" class="headerlink" title="二 文档打分的运作机制：TF-IDF"></a>二 文档打分的运作机制：TF-IDF</h2><p><code>Lucene</code>和<code>es</code>的打分机制是一个公式。将查询作为输入，使用不同的手段来确定每一篇文档的得分，将每一个因素最后通过公式综合起来，返回该文档的最终得分。这个综合考量的过程，就是我们希望相关的文档被优先返回的考量过程。在<code>Lucene</code>和<code>es</code>中这种相关性称为得分。<br> 在开始计算得分之前，<code>es</code>使用了被搜索词条的频率和它有多常见来影响得分，从两个方面理解：</p>
<ul>
<li>一个词条在某篇文档中出现的次数越多，该文档就越相关。</li>
<li>一个词条如果在不同的文档中出现的次数越多，它就越不相关！</li>
</ul>
<p>我们称之为<code>TF-IDF</code>，<code>TF</code>是词频（term frequency），而<code>IDF</code>是逆文档频率（inverse document frequency）。</p>
<h3 id="2-1-词频：TF"><a href="#2-1-词频：TF" class="headerlink" title="2.1 词频：TF"></a>2.1 词频：TF</h3><p>考虑一篇文档得分的首要方式，是查看一个词条在文档中出现的次数，比如某篇文章围绕<code>es</code>的打分展开的，那么文章中肯定会多次出现相关字眼，当查询时，我们认为该篇文档更符合，所以，这篇文档的得分会更高。<br> 闲的蛋疼的可以<code>Ctrl + f</code>搜一下相关的关键词（es，得分、打分）之类的试试。</p>
<h3 id="2-2-逆文档频率：IDF"><a href="#2-2-逆文档频率：IDF" class="headerlink" title="2.2 逆文档频率：IDF"></a>2.2 逆文档频率：IDF</h3><p>相对于词频，逆文档频率稍显复杂，如果一个词条在索引中的不同文档中出现的次数越多，那么它就越不重要。<br> 来个例子，示例<a href="http://www.kekenet.com/read/201904/583147.shtml" target="_blank" rel="noopener">地址</a>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">The rules-which require employees to work from 9 am to 9 pm</span><br><span class="line">In the weeks that followed the creation of 996.ICU in March</span><br><span class="line">The 996.ICU page was soon blocked on multiple platforms including the messaging tool WeChat and the UC Browser.</span><br></pre></td></tr></table></figure>

<p>假如<code>es</code>索引中，有上述3篇文档：</p>
<ul>
<li>词条<code>ICU</code>的文档频率是<code>2</code>，因为它出现在2篇文档中，文档的逆源自得分乘以<code>1/DF</code>，<code>DF</code>是该词条的文档频率，这就意味着，由于<code>ICU</code>词条拥有更高的文档频率，所以，它的权重会降低。</li>
<li>词条<code>the</code>的文档频率是<code>3</code>，它在3篇文档中都出现了，注意：<strong>尽管<code>the</code>在后两篇文档出都出现两次，但是它的词频是还是<code>3</code>，因为，逆文档词频只检查词条是否出现在某篇文档中，而不检查它在这篇文档中出现了多少次，那是词频该干的事儿</strong>。</li>
</ul>
<p>逆文档词频是一个重要的因素，用来平衡词条的词频。比如我们搜索<code>the 996.ICU</code>。单词<code>the</code>几乎出现在所有的文档中（中文中比如<code>的</code>），如果这个鬼东西要不被均衡一下，那么<code>the</code>的频率将完全淹没<code>996.ICU</code>。所以，逆文档词频就有效的均衡了<code>the</code>这个常见词的相关性影响。以达到实际的相关性得分将会对查询的词条有一个更准确地描述。<br> 当词频和逆文档词频计算完成。就可以使用<code>TF-IDF</code>公式来计算文档的得分了。</p>
<h2 id="三-Lucene评分公式"><a href="#三-Lucene评分公式" class="headerlink" title="三 Lucene评分公式"></a>三 Lucene评分公式</h2><p>之前的讨论<code>Lucene</code>默认评分公式被称为<code>TF-IDF</code>，一个基于词频和逆文档词频的公式。<code>Lucene</code>实用评分公式如下：</p>
<p><img src="https://img2018.cnblogs.com/blog/1168165/201904/1168165-20190417002013739-30143345.png" alt="img"></p>
<p>你以为我会着重介绍这个该死的公式？！<br> 我只能说，词条的词频越高，得分越高；相似地，索引中词条越罕见，逆文档频率越高，其中再加商调和因子和查询标准化，调和因子考虑了搜索过多少文档以及发现了多少词条；查询标准化，是试图让不同的查询结果具有可比性，这显然…..很困难。<br> 我们称这种默认的打分方法是<code>TF-IDF</code>和向量空间模型（vector space model）的结合。</p>
<h2 id="四-其他的打分方法"><a href="#四-其他的打分方法" class="headerlink" title="四 其他的打分方法"></a>四 其他的打分方法</h2><p>除了<code>TF-IDF</code>结合向量空间模型的实用评分模式，是<code>es</code>和<code>Lucene</code>最为主流的评分机制，但这并不是唯一的，除了<code>TF-IDF</code>这种实用模型之外，其他的模型包括：</p>
<ul>
<li>Okapi BM25。</li>
<li>随机性分歧（Divergence from randomness），即DFR相似度。</li>
<li>LM Dirichlet相似度。</li>
<li>LM Jelinek Mercer相似度。</li>
</ul>
<p>这里简要的介绍<code>BM25</code>几种主要设置，即<code>k1</code>、<code>b</code>和<code>discount_overlaps</code>：</p>
<ul>
<li>k1和b是数值的设置，用于调整得分是如何计算的。</li>
<li>k1控制对于得分而言词频（TF）的重要性。</li>
<li>b是介于<code>0 ~ 1</code>之间的数值，它控制了文档篇幅对于得分的影响程度。</li>
<li>默认情况下，<code>k1</code>设置为<code>1.2</code>，而<code>b</code>则被设置为<code>0.75</code></li>
<li><code>discount_overlaps</code>的设置用于告诉<code>es</code>，在某个字段中，多少个分词出现在同一位置，是否应该影响长度的标准化，默认值是<code>true</code>。</li>
</ul>
<h2 id="五-配置打分模型"><a href="#五-配置打分模型" class="headerlink" title="五 配置打分模型"></a>五 配置打分模型</h2><h3 id="5-1-简要配置BM25打分模型"><a href="#5-1-简要配置BM25打分模型" class="headerlink" title="5.1 简要配置BM25打分模型"></a>5.1 简要配置BM25打分模型</h3><p><code>BM25</code>（是不是跟pm2.5好像！！！）是一种基于概率的打分框架。我们来简要的配置一下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">PUT w2</span><br><span class="line">&#123;</span><br><span class="line">  &quot;mappings&quot;: &#123;</span><br><span class="line">    &quot;doc&quot;: &#123;</span><br><span class="line">      &quot;properties&quot;: &#123;</span><br><span class="line">        &quot;title&quot;: &#123;</span><br><span class="line">          &quot;type&quot;: &quot;text&quot;,</span><br><span class="line">          &quot;similarity&quot;: &quot;BM25&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w2&#x2F;doc&#x2F;1</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;The rules-which require employees to work from 9 am to 9 pm&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w2&#x2F;doc&#x2F;2</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;In the weeks that followed the creation of 996.ICU in March&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w2&#x2F;doc&#x2F;3</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;The 996.ICU page was soon blocked on multiple platforms including the messaging tool WeChat and the UC Browser.&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">GET w2&#x2F;doc&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;match&quot;: &#123;</span><br><span class="line">      &quot;title&quot;: &quot;the 996&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上例是通过<code>similarity</code>参数来指定打分模型。至于查询，还是当数据量比较大的时候，多试几次，比较容易发现不同之处。</p>
<h3 id="5-2-为BM25配置高级的settings"><a href="#5-2-为BM25配置高级的settings" class="headerlink" title="5.2 为BM25配置高级的settings"></a>5.2 为BM25配置高级的settings</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">PUT w3</span><br><span class="line">&#123;</span><br><span class="line">  &quot;settings&quot;: &#123;</span><br><span class="line">    &quot;index&quot;: &#123;</span><br><span class="line">      &quot;analysis&quot;: &#123;</span><br><span class="line">        &quot;analyzer&quot;:&quot;ik_smart&quot;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    &quot;similarity&quot;: &#123;</span><br><span class="line">      &quot;my_custom_similarity&quot;: &#123;</span><br><span class="line">        &quot;type&quot;: &quot;BM25&quot;,</span><br><span class="line">        &quot;k1&quot;: 1.2,</span><br><span class="line">        &quot;b&quot;: 0.75,</span><br><span class="line">        &quot;discount_overlaps&quot;: false</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;mappings&quot;: &#123;</span><br><span class="line">    &quot;doc&quot;: &#123;</span><br><span class="line">      &quot;properties&quot;: &#123;</span><br><span class="line">        &quot;title&quot;: &#123;</span><br><span class="line">          &quot;type&quot;: &quot;text&quot;,</span><br><span class="line">          &quot;similarity&quot;:&quot;my_custom_similarity&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w3&#x2F;doc&#x2F;1</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;The rules-which require employees to work from 9 am to 9 pm&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w3&#x2F;doc&#x2F;2</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;In the weeks that followed the creation of 996.ICU in March&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w3&#x2F;doc&#x2F;3</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;The 996.ICU page was soon blocked on multiple platforms including the messaging tool WeChat and the UC Browser.&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">GET w3&#x2F;doc&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;match&quot;: &#123;</span><br><span class="line">      &quot;title&quot;: &quot;the 996&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="5-3-配置全局打分模型"><a href="#5-3-配置全局打分模型" class="headerlink" title="5.3 配置全局打分模型"></a>5.3 配置全局打分模型</h3><p>如果我们要使用某种特定的打分模型，并且希望应用到全局，那么就在<code>elasticsearch.yml</code>配置文件中加入：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">index.similarity.default.type: BM25</span><br></pre></td></tr></table></figure>

<h2 id="六-boosting"><a href="#六-boosting" class="headerlink" title="六 boosting"></a>六 boosting</h2><p><code>boosting</code>是一个用来修改文档相关性的程序。<code>boosting</code>有两种类型：</p>
<ul>
<li>索引的时候，比如我们在定义mappings的时候。</li>
<li>查询一篇文档的时候。</li>
</ul>
<p>以上两种方式都可以提升一个篇文档的得分。需要注意的是：<strong>在索引期间修改的文档boosting是存储在索引中的，要想修改boosting必须重新索引该篇文档</strong>。</p>
<h3 id="6-1-索引期间的boosting"><a href="#6-1-索引期间的boosting" class="headerlink" title="6.1 索引期间的boosting"></a>6.1 索引期间的boosting</h3><p>啥也不说了，都在酒里！上代码：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">PUT w4</span><br><span class="line">&#123;</span><br><span class="line">  &quot;mappings&quot;: &#123;</span><br><span class="line">    &quot;doc&quot;: &#123;</span><br><span class="line">      &quot;properties&quot;: &#123;</span><br><span class="line">        &quot;name&quot;: &#123;</span><br><span class="line">          &quot;boost&quot;: 2.0,</span><br><span class="line">          &quot;type&quot;: &quot;text&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;age&quot;: &#123;</span><br><span class="line">          &quot;type&quot;: &quot;long&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>一劳永逸是没错，但一般不推荐这么玩。</p>
<p>原因之一是因为一旦映射建立完成，那么所有<code>name</code>字段都会自动拥有一个<code>boost</code>值。要想修改这个值，那就必须重新索引文档。<br> 另一个原因是，<code>boost</code>值是以降低精度的数值存储在<code>Lucene</code>内部的索引结构中。只有一个字节用于存储浮点型数值（存不下就损失精度了），所以，计算文档的最终得分时可能会损失精度。<br> 最后，<code>boost</code>是应用与词条的。因此，再被<code>boost</code>的字段中如果匹配上了多个词条，就意味着计算多次的<code>boost</code>，这将会进一步增加字段的权重，可能会影响最终的文档得分。<br> 现在我们再来介绍另一种方式。</p>
<h3 id="6-2-查询期间的boosting"><a href="#6-2-查询期间的boosting" class="headerlink" title="6.2 查询期间的boosting"></a>6.2 查询期间的boosting</h3><p>在<code>es</code>中，几乎所有的查询类型都支持<code>boost</code>，正如你想象的那些<code>match、multi_match</code>等等。<br> 来个示例，在查询期间，使用match查询进行<code>boosting</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line">PUT w5</span><br><span class="line">&#123;</span><br><span class="line">  &quot;mappings&quot;:&#123;</span><br><span class="line">    &quot;doc&quot;:&#123;</span><br><span class="line">      &quot;properties&quot;: &#123;</span><br><span class="line">        &quot;title&quot;: &#123;</span><br><span class="line">          &quot;type&quot;: &quot;text&quot;,</span><br><span class="line">          &quot;analyzer&quot;: &quot;ik_max_word&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;content&quot;: &#123;</span><br><span class="line">          &quot;type&quot;: &quot;text&quot;,</span><br><span class="line">          &quot;analyzer&quot;: &quot;ik_max_word&quot;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w5&#x2F;doc&#x2F;1</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;Lucene is cool&quot;,</span><br><span class="line">  &quot;content&quot;: &quot;Lucene is cool&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w5&#x2F;doc&#x2F;2</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;Elasticsearch builds on top of lucene&quot;,</span><br><span class="line">  &quot;content&quot;:&quot;Elasticsearch builds on top of lucene&quot;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">PUT w5&#x2F;doc&#x2F;3</span><br><span class="line">&#123;</span><br><span class="line">  &quot;title&quot;:&quot;Elasticsearch rocks&quot;,</span><br><span class="line">  &quot;content&quot;:&quot;Elasticsearch rocks&quot;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>来查询：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">GET w5&#x2F;doc&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;bool&quot;: &#123;</span><br><span class="line">      &quot;should&quot;: [</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;match&quot;: &#123;</span><br><span class="line">            &quot;title&quot;:&#123;</span><br><span class="line">              &quot;query&quot;: &quot;elasticserach rocks&quot;,</span><br><span class="line">              &quot;boost&quot;: 2.5</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          &quot;match&quot;: &#123;</span><br><span class="line">            &quot;content&quot;: &quot;elasticserach rocks&quot;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>就对于最终得分而言，<code>content</code>字段，加了<code>boost</code>的<code>title</code>查询更有影响力。也只有在<code>bool</code>查询中，<code>boost</code>更有意义。</p>
<h3 id="6-3-跨越多个字段的查询"><a href="#6-3-跨越多个字段的查询" class="headerlink" title="6.3 跨越多个字段的查询"></a>6.3 跨越多个字段的查询</h3><p><code>boost</code>也可以用于<code>multi_match</code>查询。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">GET w5&#x2F;doc&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;multi_match&quot;: &#123;</span><br><span class="line">      &quot;query&quot;: &quot;elasticserach rocks&quot;,</span><br><span class="line">      &quot;fields&quot;: [&quot;title&quot;, &quot;content&quot;],</span><br><span class="line">      &quot;boost&quot;: 2.5</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>除此之外，我们还可以使用特殊的语法，只为特定的字段指定一个<code>boost</code>。通过在字段名称后添加一个<code>^</code>符号和<code>boost</code>的值。告诉es只需对那个字段进行<code>boost</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">GET w5&#x2F;doc&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;multi_match&quot;: &#123;</span><br><span class="line">      &quot;query&quot;: &quot;elasticserach rocks&quot;,</span><br><span class="line">      &quot;fields&quot;: [&quot;title^3&quot;, &quot;content&quot;]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>上例中，<code>title</code>字段被<code>boost</code>了3倍。<br> 需要注意的是：在使用<code>boost</code>的时候，无论是字段或者词条，都是按照相对值来<code>boost</code>的，而不是乘以乘数。如果对于所有的待搜索词条<code>boost</code>了同样的值，那么就好像没有<code>boost</code>一样（废话，就像大家都同时长高一米似的）！因为Lucene会标准化<code>boost</code>的值。如果<code>boost</code>一个字段<code>4</code>倍，不是意味着该字段的得分就是乘以<code>4</code>的结果。所以，如果你的得分不是按照严格的乘法结果，也不要担心。</p>
<h2 id="七-使用“解释”来理解文档是如何评分的"><a href="#七-使用“解释”来理解文档是如何评分的" class="headerlink" title="七 使用“解释”来理解文档是如何评分的"></a>七 使用“解释”来理解文档是如何评分的</h2><p>一切都不是你想的那样！是的，在<code>es</code>中，一个文档要比另一个文档更符合某个查询很可能跟我们想象的不太一样！<br> 这一小节，我们来研究下<code>es</code>和<code>Lucene</code>内部使用了怎样的公式来计算得分。<br> 我们通过<code>explain=true</code>来告诉<code>es</code>，你要给洒家解释一下为什么这个得分是这样的？！背后到底以有什么py交易！<br> 比如我们来查询：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">GET py1&#x2F;doc&#x2F;_search</span><br><span class="line">&#123;</span><br><span class="line">  &quot;query&quot;: &#123;</span><br><span class="line">    &quot;match&quot;: &#123;</span><br><span class="line">      &quot;title&quot;: &quot;北京&quot;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;explain&quot;: true,</span><br><span class="line">  &quot;_source&quot;: &quot;title&quot;, </span><br><span class="line">  &quot;size&quot;: 1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>由于结果太长，我们这里对结果进行了过滤（<code>&quot;size&quot;: 1</code>返回一篇文档），只查看指定的字段（<code>&quot;_source&quot;: &quot;title&quot;</code>只返回<code>title</code>字段）。<br> 看结果：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;took&quot; : 1,</span><br><span class="line">  &quot;timed_out&quot; : false,</span><br><span class="line">  &quot;_shards&quot; : &#123;</span><br><span class="line">    &quot;total&quot; : 5,</span><br><span class="line">    &quot;successful&quot; : 5,</span><br><span class="line">    &quot;skipped&quot; : 0,</span><br><span class="line">    &quot;failed&quot; : 0</span><br><span class="line">  &#125;,</span><br><span class="line">  &quot;hits&quot; : &#123;</span><br><span class="line">    &quot;total&quot; : 24,</span><br><span class="line">    &quot;max_score&quot; : 4.9223156,</span><br><span class="line">    &quot;hits&quot; : [</span><br><span class="line">      &#123;</span><br><span class="line">        &quot;_shard&quot; : &quot;[py1][1]&quot;,</span><br><span class="line">        &quot;_node&quot; : &quot;NRwiP9PLRFCTJA7w3H9eqA&quot;,</span><br><span class="line">        &quot;_index&quot; : &quot;py1&quot;,</span><br><span class="line">        &quot;_type&quot; : &quot;doc&quot;,</span><br><span class="line">        &quot;_id&quot; : &quot;NIjS1mkBuoj17MYtV-dX&quot;,</span><br><span class="line">        &quot;_score&quot; : 4.9223156,</span><br><span class="line">        &quot;_source&quot; : &#123;</span><br><span class="line">          &quot;title&quot; : &quot;大写的尴尬 插混为啥在北京不受待见？&quot;</span><br><span class="line">        &#125;,</span><br><span class="line">        &quot;_explanation&quot; : &#123;</span><br><span class="line">          &quot;value&quot; : 4.9223156,</span><br><span class="line">          &quot;description&quot; : &quot;weight(title:北京 in 36) [PerFieldSimilarity], result of:&quot;,</span><br><span class="line">          &quot;details&quot; : [</span><br><span class="line">            &#123;</span><br><span class="line">              &quot;value&quot; : 4.9223156,</span><br><span class="line">              &quot;description&quot; : &quot;score(doc&#x3D;36,freq&#x3D;1.0 &#x3D; termFreq&#x3D;1.0\n), product of:&quot;,</span><br><span class="line">              &quot;details&quot; : [</span><br><span class="line">                &#123;</span><br><span class="line">                  &quot;value&quot; : 4.562031,</span><br><span class="line">                  &quot;description&quot; : &quot;idf, computed as log(1 + (docCount - docFreq + 0.5) &#x2F; (docFreq + 0.5)) from:&quot;,</span><br><span class="line">                  &quot;details&quot; : [</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 4.0,</span><br><span class="line">                      &quot;description&quot; : &quot;docFreq&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 430.0,</span><br><span class="line">                      &quot;description&quot; : &quot;docCount&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;</span><br><span class="line">                  ]</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                  &quot;value&quot; : 1.0789746,</span><br><span class="line">                  &quot;description&quot; : &quot;tfNorm, computed as (freq * (k1 + 1)) &#x2F; (freq + k1 * (1 - b + b * fieldLength &#x2F; avgFieldLength)) from:&quot;,</span><br><span class="line">                  &quot;details&quot; : [</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 1.0,</span><br><span class="line">                      &quot;description&quot; : &quot;termFreq&#x3D;1.0&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 1.2,</span><br><span class="line">                      &quot;description&quot; : &quot;parameter k1&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 0.75,</span><br><span class="line">                      &quot;description&quot; : &quot;parameter b&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 12.1790695,</span><br><span class="line">                      &quot;description&quot; : &quot;avgFieldLength&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;,</span><br><span class="line">                    &#123;</span><br><span class="line">                      &quot;value&quot; : 10.0,</span><br><span class="line">                      &quot;description&quot; : &quot;fieldLength&quot;,</span><br><span class="line">                      &quot;details&quot; : [ ]</span><br><span class="line">                    &#125;</span><br><span class="line">                  ]</span><br><span class="line">                &#125;</span><br><span class="line">              ]</span><br><span class="line">            &#125;</span><br><span class="line">          ]</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>在新增的<code>_explanation</code>字段中，可以看到<code>value</code>值是<code>4.9223156</code>，那么是怎么算出来的呢？<br> 来分析，分词<code>北京</code>在描述字段（title）出现了<code>1</code>次，所以<code>TF</code>的综合得分经过<code>&quot;description&quot; : &quot;tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:&quot;</code>计算，得分是<code>1.0789746</code>。<br> 那么逆文档词频呢？根据<code>&quot;description&quot; : &quot;idf, computed as log(1 + (docCount - docFreq + 0.5) / (docFreq + 0.5)) from:&quot;</code>计算得分是<code>4.562031</code>。<br> 所以最终得分是：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">1.0789746 * 4.562031 &#x3D; 4.9223155734126</span><br></pre></td></tr></table></figure>

<p>结果在四舍五入后就是<code>4.9223156</code>。<br> 需要注意的是，<code>explain</code>的特性会给<code>es</code>带来额外的性能开销。所以，除了在调试时可以使用，生产环境下，应避免使用<code>explain</code>。</p>
</section>
    <!-- Tags START -->
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/db/Redis%E7%B3%BB%E5%88%97/09-Redis%E7%B3%BB%E5%88%97%E4%B9%8B-%E7%BC%93%E5%AD%98%E7%9A%84%E4%BD%BF%E7%94%A8%E5%92%8C%E4%BC%98%E5%8C%96/">
        <span class="nav-arrow">← </span>
        
          db/Redis系列/09-Redis系列之-缓存的使用和优化
        
      </a>
    
    
      <a class="nav-right" href="/db/Elasticsearch%E7%B3%BB%E5%88%97/20-Elasticsearch%E4%B9%8B%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF/">
        
          db/Elasticsearch系列/20-Elasticsearch之常见错误
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">点击上方按钮,请我喝杯咖啡！</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Gitment START -->
      <div id="comments"></div>
      <!-- Gitment END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
  <!-- 不蒜子统计 -->
    <strong class="toc-title">目录</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#一-例子"><span class="toc-nav-text">一 例子</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#二-文档打分的运作机制：TF-IDF"><span class="toc-nav-text">二 文档打分的运作机制：TF-IDF</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-1-词频：TF"><span class="toc-nav-text">2.1 词频：TF</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#2-2-逆文档频率：IDF"><span class="toc-nav-text">2.2 逆文档频率：IDF</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#三-Lucene评分公式"><span class="toc-nav-text">三 Lucene评分公式</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#四-其他的打分方法"><span class="toc-nav-text">四 其他的打分方法</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#五-配置打分模型"><span class="toc-nav-text">五 配置打分模型</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#5-1-简要配置BM25打分模型"><span class="toc-nav-text">5.1 简要配置BM25打分模型</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#5-2-为BM25配置高级的settings"><span class="toc-nav-text">5.2 为BM25配置高级的settings</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#5-3-配置全局打分模型"><span class="toc-nav-text">5.3 配置全局打分模型</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#六-boosting"><span class="toc-nav-text">六 boosting</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-1-索引期间的boosting"><span class="toc-nav-text">6.1 索引期间的boosting</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-2-查询期间的boosting"><span class="toc-nav-text">6.2 查询期间的boosting</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#6-3-跨越多个字段的查询"><span class="toc-nav-text">6.3 跨越多个字段的查询</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#七-使用“解释”来理解文档是如何评分的"><span class="toc-nav-text">七 使用“解释”来理解文档是如何评分的</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://www.liuqingzheng.top/db/Elasticsearch系列/18-Elasticsearch之打分机制/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', 'http://file.muyutech.com/error-img.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== 'http://file.muyutech.com/error-img.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>



  <script>
    var gitmentConfig = "liuqingzheng";
    if (gitmentConfig !== 'undefined') {
      var gitment = new Gitment({
        id: "db/Elasticsearch系列/18-Elasticsearch之打分机制",
        owner: "liuqingzheng",
        repo: "FuckBlog",
        oauth: {
          client_id: "32a4076431cf39d0ecea",
          client_secret: "94484bd79b3346a949acb2fda3c8a76ce16990c6"
        },
        theme: {
          render(state, instance) {
            const container = document.createElement('div')
            container.lang = "en-US"
            container.className = 'gitment-container gitment-root-container'
            container.appendChild(instance.renderHeader(state, instance))
            container.appendChild(instance.renderEditor(state, instance))
            container.appendChild(instance.renderComments(state, instance))
            container.appendChild(instance.renderFooter(state, instance))
            return container;
          }
        }
      })
      gitment.render(document.getElementById('comments'))
    }
  </script>




    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
<!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
     本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
     本站访客数<span id="busuanzi_value_site_uv"></span>人
</span>
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



  <p class="copyright">
    &copy; 2021 | Proudly powered by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank">小猿取经</a>
    <br>
    Theme by <a href="https://www.cnblogs.com/xiaoyuanqujing" target="_blank" rel="noopener">小猿取经</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("//cdnjs.cloudflare.com/ajax/libs/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>





<!-- Baidu Tongji -->

<script>
    var _baId = 'c5fd96eee1193585be191f318c3fa725';
    // Originial
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?" + _baId;
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
</script>


<script src="/js/script.js"></script>


<script src="/js/search.js"></script>


<script src="/js/load.js"></script>



  <span class="local-search local-search-google local-search-plugin" style="right: 50px;top: 70px;;position:absolute;z-index:2;">
      <input type="search" placeholder="站内搜索" id="local-search-input" class="local-search-input-cls" style="">
      <div id="local-search-result" class="local-search-result-cls"></div>
  </span>


  </body>
</html>